package com.eflow.model.flowable.converter;

import cn.hutool.core.bean.BeanUtil;
import com.eflow.model.domain.EflowUser;
import com.eflow.model.e.BpmConst;
import com.eflow.model.flowable.model.RelativeElement;
import com.eflow.model.flowable.model.UserElement;
import com.eflow.model.flowable.model.e.BaseEnum;
import com.eflow.model.flowable.model.e.RelativeDeptType;
import com.eflow.model.flowable.model.e.UserTypeEnum;
import com.eflow.model.service.IEflowUserService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author hui se
 * @description
 * @create 2021-03-05 18:21
 **/
@Component
@Slf4j
public class UserRelativeConverter extends BaseUserListConverter implements BpmConst {

    @Autowired
    private IEflowUserService eflowUserService;


    @Override
    List<EflowUser> list(UserElement userElement, String loginUser, ProcessDefinition processDefinition, String processInstanceId, String activityId, Map<String,Object> var) {
        Map data = (Map)userElement.getData();
        RelativeElement relativeElement = new RelativeElement();
        BeanUtil.fillBeanWithMap(data, relativeElement, true);
        RelativeDeptType e = BaseEnum.codeOf(relativeElement.getType(), RelativeDeptType.class);
        String userId=null;
        switch (e) {
            case START_USER:
                userId = var.get(FLOW_SUBMIT_TASK_VAL).toString();
                break;
            case APPROVE_USER:
                userId = loginUser;
                break;
            case PROCESS_VAR:
               String deptCode =  var.get(relativeElement.getField()).toString();
                return eflowUserService.selectDeptPostUser(deptCode,relativeElement.getPostId());
            default:
               log.debug("获取相对用户未找到对应类型:{}",e);
               return Collections.emptyList();
        }
        return eflowUserService.selectUserDeptPostUser(userId,relativeElement.getPostId());
    }

    @Override
    UserTypeEnum getUserType() {
        return UserTypeEnum.RELATIVELY;
    }
}
